import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router';
import Home from '@/views/home/Home.vue';
import R404 from '@/views/r404/R404.vue';
import Login from '@/views/login/Login.vue';
import store from '../store/index';
import {CCRouter, RouteNameEnum} from '@/model/RouterClass';
// @ts-ignore
import nProgress from 'nprogress';
import PersonalCenter from '@/views/personal-center/PersonalCenter.vue';
import Test from '@/views/cc-manager/test-view/Test.vue';

const whiteRouteNameList: string [] = [
  RouteNameEnum.login.toString(),
  RouteNameEnum.test.toString(),
  RouteNameEnum.r404.toString(),
  RouteNameEnum.r401.toString(),
];
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: {
      name: RouteNameEnum.home,
    },
  },
  {
    path: '/home',
    name: RouteNameEnum.home,
    component: Home,
    // children: [
    //   {
    //     path: '/testView1',
    //     name: RouteNameEnum.test,
    //     component: Test,
    //   },
    // ],
  },
  {
    path: '/login',
    name: RouteNameEnum.login,
    component: Login,
  },
  {
    path: '/404',
    name: RouteNameEnum.r404,
    component: R404,
  },
  {
    path: '/:catchAll(.*)',
    redirect: {
      name: RouteNameEnum.r404,
    },
  },
];
const router = createRouter({
  history: createWebHistory(''),
  routes,
});
const ccRouter: CCRouter = new CCRouter();
ccRouter.router = router;
const userTokenNotNull = (): boolean => {
  return  store.getters['user/token'] && store.getters['user/token'] !== 'undefined' && store.getters['user/token'] !== 'null' && store.getters['user/token'] !== '';
};
router.beforeEach((to, from, next) => {
  nProgress.start();
  if (whiteRouteNameList.includes(to.name as string)) {
    next();
  } else {
    if (userTokenNotNull()) {
      next();
      // if (store.getters['user/permissions'].includes(to.name)) {
      //   next();
      // } else {
      //   next({ name: routeNameEnum.r401 });
      // }
    } else {
      next({ name: RouteNameEnum.login });
    }
  }
  nProgress.done();
});

export default ccRouter;
